# -*- coding: utf-8 -*-
from fastapi   import Request, APIRouter
from src.types import Account

from fastapi.responses  import HTMLResponse, FileResponse, RedirectResponse


index = APIRouter()


@index.get("/favicon.ico", include_in_schema = False)
async def favicon():
    return FileResponse("./stack/favicon.ico")


@index.get("/", response_class = HTMLResponse)
async def main(request: Request):
    fullname = None

    if request.cookies.get("id") != None:
        database = request.app.database
        account  = await Account.get_by_id(id = request.cookies.get("id"), database = database)

        fullname = account.fullName

    return request.app.template.TemplateResponse(
        request = request, name = "index.html", context = {"user": fullname})


@index.get("/login", response_class = HTMLResponse)
async def login(request: Request):
    if request.cookies.get("id") != None:
        return RedirectResponse("/")

    return request.app.template.TemplateResponse(
        request = request, name = "login.html", context = {"user": None})

@index.get("/logout", response_class = HTMLResponse)
async def logout(request: Request):
    response = RedirectResponse("/")

    if request.cookies.get("id") != None:
        response.delete_cookie("id")
    return response

@index.get("/register", response_class = HTMLResponse)
async def register(request: Request):
    if request.cookies.get("id") != None:
        return RedirectResponse("/")

    return request.app.template.TemplateResponse(
        request = request, name = "register.html", context = {"user": None})


@index.get("/accounts", response_class = HTMLResponse)
async def accounts(request: Request):
    if request.cookies.get("id") == None:
        return RedirectResponse("/")

    database = request.app.database
    account  = await Account.get_by_id(id = request.cookies["id"], database = database)

    return request.app.template.TemplateResponse(
        request = request, name = "accounts.html", context = {"user": account.fullName})

@index.get("/regions", response_class = HTMLResponse)
async def regions(request: Request):
    if request.cookies.get("id") == None:
        return RedirectResponse("/")

    database = request.app.database
    account  = await Account.get_by_id(id = request.cookies["id"], database = database)

    return request.app.template.TemplateResponse(
        request = request, name = "regions.html", context = {"user": account.fullName})


@index.get("/region", response_class = HTMLResponse)
async def region(request: Request):
    if request.cookies.get("id") == None:
        return RedirectResponse("/")

    database = request.app.database
    account  = await Account.get_by_id(id = request.cookies["id"], database = database)

    return request.app.template.TemplateResponse(
        request = request, name = "region.html", context = {"user": account.fullName})

@index.get("/region-types", response_class = HTMLResponse)
async def region_types(request: Request):
    if request.cookies.get("id") == None:
        return RedirectResponse("/")

    database = request.app.database
    account  = await Account.get_by_id(id = request.cookies["id"], database = database)

    return request.app.template.TemplateResponse(
        request = request, name = "region-types.html", context = {"user": account.fullName})


@index.get("/map", response_class = HTMLResponse)
async def map_index(request: Request):
    fullname = None

    if request.cookies.get("id") != None:
        database = request.app.database
        account  = await Account.get_by_id(id = request.cookies.get("id"), database = database)

        fullname = account.fullName

    return request.app.template.TemplateResponse(
        request = request, name = "map.html", context = {"user": fullname})
